#!/bin/sh
#################################
# use for fun. created by agate #
# contact: agate.hao@gmail.com  #
# website: http://agatezone.cn  #
#################################

PRG="$0"

while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi
done

DIRNAME=`dirname $PRG`

MYSQL_HOME=`cd $DIRNAME; pwd`

f1=$MYSQL_HOME/mysql-startup
f2=$MYSQL_HOME/mysql-shutdown
f3=$MYSQL_HOME/mysql-view-log
f4=$MYSQL_HOME/mysql-del-log

fcnf=$MYSQL_HOME/my.cnf

for f in $f1 $f2 $f3 $f4; do
	sed 's#MYSQL_HOME=.*#'"MYSQL_HOME=$MYSQL_HOME#g" "$f" > ./temp-sh
	mv ./temp-sh $f
	chmod +x $f
	if [ $? -ne 0 ]; then
		echo "Error updating files for new installation path."
		echo "Make sure you are in the path called \"mysql-green-linux\""
		exit 1
	fi
done
sed 's#basedir = .*#'"basedir = $MYSQL_HOME#g" "$fcnf" > ./temp-cnf
mv ./temp-cnf $fcnf

mkdir temp
for f in $f1 $f2 $f3 $f4; do
	ln -s $f temp
done
sudo mv ./temp/* /usr/bin/
rm temp -r

echo "##########################################"
echo "#            update path done            #"
echo "##########################################"

## prepared database ##
./scripts/mysql_install_db

echo "##########################################"
echo "#           init database done           #"
echo "##########################################"
